package com.ecoolex.hotel.saas.core.manager;

import com.ecoolex.framework.common.util.BeanUtil;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.hotel.saas.core.dao.StoreWeixinConfigDAO;
import com.ecoolex.hotel.saas.entity.StoreWeixinConfig;
import com.ecoolex.hotel.saas.entity.StoreWeixinConfig.Column;
import com.ecoolex.hotel.saas.entity.StoreWeixinConfigExample;
import com.ecoolex.hotel.saas.entity.StoreWeixinConfigExample.Criteria;
import com.ecoolex.hotel.saas.pojo.EditStoreWeixinConfigDTO;

import java.time.Instant;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class StoreWeixinConfigManager {
	
    @Autowired
    private StoreWeixinConfigDAO dao;
    
    public StoreWeixinConfig getStoreWeixinConfig(Integer storeId) {
		return dao.selectByPrimaryKeyWithLogicalDelete(storeId, StoreWeixinConfig.NOT_DELETED);
	}
    
    public List<StoreWeixinConfig> query(){
    	StoreWeixinConfigExample example = new StoreWeixinConfigExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andDeletedEqualTo(StoreWeixinConfig.NOT_DELETED);
    	return dao.selectByExample(example);
    }

	public StoreWeixinConfig editStoreWeixinConfig(EditStoreWeixinConfigDTO edit, String updateUser){
		StoreWeixinConfig config = getStoreWeixinConfig(edit.getStoreId());
		if(Check.notNull(config)) {
			config = BeanUtil.copy(edit, config);
			config.setUpdateUser(updateUser);
			config.setUpdateTime(Date.from(Instant.now()));
			dao.updateByPrimaryKeySelective(config, Column.excludes(Column.deleted,Column.createUser,Column.createTime));
		}else {
			config = new StoreWeixinConfig();
			config = BeanUtil.copy(edit, config);
			config.setDeleted(StoreWeixinConfig.NOT_DELETED);
			config.setCreateTime(Date.from(Instant.now()));
			config.setCreateUser(updateUser);
			config.setUpdateUser(updateUser);
			config.setUpdateTime(Date.from(Instant.now()));
			dao.insert(config);
		}
		return config;
	}
}